//把配置项单独处理
import configs from '../config/wap.js'
// import store from '/store/index.js'; //vuex  
let server_url = configs.requestUrl; //请求地址
let token = ''; //凭证
// process.env.NODE_ENV === 'development' ? '192.168.0.1' : 'http://***/api' ; //环境配置
function service(options = {}) {
	token = uni.getStorageSync('jun-token') ? uni.getStorageSync('jun-token') : ''
	// store.state.token && (token = store.state.token); //从vuex中获取登录凭证
	options.url = `${server_url}${options.url}`;
	//配置请求头
	options.header = {
		// 'content-type': 'application/x-www-form-urlencoded',
		'jun-token': `${token}`, //Bearer 
		'shop-lang': uni.getLocale()
	};
	options.method = options.method ? options.method : 'GET'
	uni.showLoading({})
	return new Promise((resolved, rejected) => {
		//成功
		options.success = (res) => {
			uni.hideLoading()
			if (res.data.code == 0) {
				resolved(res.data);
			} else if (res.data.code == 400) {
				uni.removeStorageSync('jun-token', '')
				uni.redirectTo({
					url: '/pages/login/index'
				})
				// uni.$showMsg(res.data.msg)
				// setTimeout(() => {
				// 	uni.redirectTo({
				// 		url: '/pages/login/index'
				// 	})
				// }, 500)
				// resolved(res.data);
				return false
			} else {
				uni.$showMsg(res.data.msg)
				resolved(res.data);
			}
		}
		//错误
		options.fail = (err) => {
			uni.hideLoading()
			console.log('err', err);
			rejected(err); //错误
		}
		uni.request(options);

	});
}
export default service;